package cn.iocoder.yudao.module.game.controller.admin.platformData;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.platformData.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.platformData.PlatformDataDO;
import cn.iocoder.yudao.module.game.service.platformData.PlatformDataService;

@Tag(name = "管理后台 - 平台数据")
@RestController
@RequestMapping("/game/platform-data")
@Validated
public class PlatformDataController {

    @Resource
    private PlatformDataService platformDataService;

    @PostMapping("/create")
    @Operation(summary = "创建平台数据")
    @PreAuthorize("@ss.hasPermission('game:platform-data:create')")
    public CommonResult<Long> createPlatformData(@Valid @RequestBody PlatformDataSaveReqVO createReqVO) {
        return success(platformDataService.createPlatformData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台数据")
    @PreAuthorize("@ss.hasPermission('game:platform-data:update')")
    public CommonResult<Boolean> updatePlatformData(@Valid @RequestBody PlatformDataSaveReqVO updateReqVO) {
        platformDataService.updatePlatformData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:platform-data:delete')")
    public CommonResult<Boolean> deletePlatformData(@RequestParam("id") Long id) {
        platformDataService.deletePlatformData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:platform-data:query')")
    public CommonResult<PlatformDataRespVO> getPlatformData(@RequestParam("id") Long id) {
        PlatformDataDO platformData = platformDataService.getPlatformData(id);
        return success(BeanUtils.toBean(platformData, PlatformDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台数据分页")
    @PreAuthorize("@ss.hasPermission('game:platform-data:query')")
    public CommonResult<PageResult<PlatformDataRespVO>> getPlatformDataPage(@Valid PlatformDataPageReqVO pageReqVO) {
        PageResult<PlatformDataDO> pageResult = platformDataService.getPlatformDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台数据 Excel")
    @PreAuthorize("@ss.hasPermission('game:platform-data:export')")
    @OperateLog(type = EXPORT)
    public void exportPlatformDataExcel(@Valid PlatformDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformDataDO> list = platformDataService.getPlatformDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台数据.xls", "数据", PlatformDataRespVO.class,
                        BeanUtils.toBean(list, PlatformDataRespVO.class));
    }

}